﻿using Super_Management_Mayhem.Content.Spritesheet;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.ScaleAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Menu.CreateAvatarName
{
    public class Keyboard : Container
    {
        private ImageContainer _cursorImage;
        private int _cursorX, _cursorY;
        private TE_Vector2 _letterOffset;
        private static readonly string[,] Letters = new[,] {
                                                               { "A", "B", "C", "D", "E", "F", "G", "H" },
                                                               { "I", "J", "K", "L", "M", "N", "O", "P" },
                                                               { "Q", "R", "S", "T", "U", "V", "W", "X" },
                                                               { "Y", "Z", "@", "@", "@", "@", "@", "@" },
                                                               { "a", "b", "c", "d", "e", "f", "g", "h" },
                                                               { "i", "j", "k", "l", "m", "n", "o", "p" },
                                                               { "q", "r", "s", "t", "u", "v", "w", "x" },
                                                               { "y", "z", "_", "@", "@", "@", "<", "Enter" },
                                                           };

        public string CurrentKey
        {
            get
            {
                return Letters[_cursorY, _cursorX];
            }
        }

        public Keyboard(TE_Vector2 size)
        {
            var letterContainer = new Container(2, 2);
            var keyboardSize = size.Offset(-4, -4);
            _letterOffset = new TE_Vector2(keyboardSize.X / (Letters.GetLength(1) - 1), keyboardSize.Y / (Letters.GetLength(0) - 1));
            _cursorX = 0;
            _cursorY = 0;

            for (int y = 0; y < Letters.GetLength(0); y++)
            {
                for (int x = 0; x < Letters.GetLength(1); x++)
                {
                    if(Letters[y, x] != "@" && 
                        Letters[y, x] != "Enter")
                    {
                        letterContainer.AddChildren(new Label(MenuSheet.GetInstance().GetSpriteAlphabet<MenuSheet.LetterAlphabet>, Letters[y, x]) {
                                                                                                                                          PositionGridBound = new TE_Vector2(_letterOffset.X * x, _letterOffset.Y * y),
                                                                                                                                          Origin = TE_Rectangle.Alignment.CenterMiddle,
                                                                                                                                          Scale = new TE_Vector2(2, 2)
                                                                                                                                      });
                    }
                }
            }

            _cursorImage = new ImageContainer {
                                                  Sprite = MenuSheet.GetSprite(MenuSheet.Menu.LetterSelected),
                                                  Origin = TE_Rectangle.Alignment.CenterMiddle
                                              };
            _cursorImage.AddAnimations(new ScaleAnimation(new SinusTrajectory(), new TE_Vector2(1.25f, 1.25f), 800, true, 0));
            

            var enter = new ImageContainer {
                                                PositionGridBound = new TE_Vector2(_letterOffset.X * (Letters.GetLength(0) - 1), _letterOffset.Y * (Letters.GetLength(1) - 1)),
                                                Sprite = MenuSheet.GetSprite(MenuSheet.Menu.Enter),
                                                Origin = TE_Rectangle.Alignment.CenterMiddle
                                            };
            letterContainer.AddChildren(enter, _cursorImage);
            AddChildren(new KeyboardBackground(size), letterContainer);
        }

        void MoveCursor()
        {
            _cursorImage.ClearAnimation<PositionAnimation>();
            _cursorImage.AddAnimations(new PositionAnimation(new QuadraticEasing(BaseTrajectory.Easing.EaseInAndOut),
                                                             new TE_Vector2(_letterOffset.X * _cursorX, _letterOffset.Y * _cursorY),
                                                             100,
                                                             false,
                                                             0));
        }

        public void MoveLeft()
        {
            do
            {
                _cursorX--;
                if(_cursorX < 0)
                    _cursorX = Letters.GetLength(1) - 1;

            } while (Letters[_cursorY, _cursorX] == "@");

            MoveCursor();
        }

        public void MoveRight()
        {
            do
            {
                _cursorX++;
                if(_cursorX >= Letters.GetLength(1))
                    _cursorX = 0;

            } while (Letters[_cursorY, _cursorX] == "@");

            MoveCursor();
        }

        public void MoveUp()
        {
            do
            {
                _cursorY--;
                if(_cursorY < 0)
                    _cursorY = Letters.GetLength(0) - 1;

            } while (Letters[_cursorY, _cursorX] == "@");

            MoveCursor();
        }

        public void MoveDown()
        {
            do
            {
                _cursorY++;
                if(_cursorY >= Letters.GetLength(0))
                    _cursorY = 0;

            } while (Letters[_cursorY, _cursorX] == "@");

            MoveCursor();
        }
    }
}